package com.pfzy.utildemo.activity.record.soundrecord.util;

import android.media.AudioRecord;
import android.util.Log;

import com.pfzy.utildemo.activity.record.soundrecord.constatns.SoundRecordConstant;

/**
 * 环境检查实用程序
 */
public class EnvironmentCheckUtil {
    public static final String TAG = "EnvironmentCheckUtil";

    public static boolean isStorageSpaceEnough() {
        return ((int) FileSizeUtil.getInternalStorageAvailableSpace()) > SoundRecordConstant.MIN_STORAGE_SPACE_MB;
    }

    /**
     * 验证麦克风可用性
     *
     * @return
     */
    public static boolean validateMicAvailability() {
        Boolean retn = true;
        AudioRecord audioRecord = null;

        try {
            audioRecord = new AudioRecord(1, 44100, 16, 1, 44100);
            if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_STOPPED) {
                retn = false;
            }

            if (retn) {
                audioRecord.startRecording();
                if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
                    retn = false;
                }
                audioRecord.stop();
            }

            try {
                if (audioRecord != null) {
                    audioRecord.release();
                }
            } catch (Exception e) {
                Log.d("EnvironmentCheckUtil", "release时 失败 = " + e.toString());
            }
        } catch (Exception e) {
            Log.d("EnvironmentCheckUtil", "validateMicAvailability() ->" + e.toString());
            e.printStackTrace();
            retn = false;
        } catch (Throwable th) {
            throw th;
        }
        return retn;
    }
}
